<?php
class Controller {
	private $data = array ();
	public $view;
	public $model;
	
	/*function __construct(){
	}*/
	function __construct() {
		if (DEBUG === TRUE) {
			echo '--- Controller construct!' . '<br/>';
		}
	}
	
	function display($tplfile = '') {
		if ($_GET ['controller'] && ! $tplfile) {
			if ($_GET ['ACTION'] != 'index') {
				$tplfile = $_GET ['controller'] . '_' . $_GET ['ACTION'] . '.htm';
			} else {
				$tplfile = $_GET ['controller'] . '.htm';
			}
		} elseif (! $_GET ['controller'] && !$tplfile) {
			exit ( 'Cound not found controller!' );
		}
		$this->getView ();
		if (! empty ( $this->data )) {
			foreach ( $this->data as $key => $value ) {
				$this->view->assign ( $key, $value );
			}
		}
		$this->view->assign ( 'global', $GLOBALS ['global'] );
		$this->view->display ( $tplfile );
	}
	
	function assign($name, $value) {
		$this->getView ();
		$this->view->assign ( $name, $value );
	}
	
	/**
	 * 创建一个View对象
	 *
	 * @return View对象
	 */
	public function getView() {
		if (! is_object ( $this->view )) {
			Mid::loadClass ( 'view' );
			$this->view = View::Instance ();
		}
		return $this->view;
	}
	
	/**
	 * 创建一个Model对象
	 *
	 * @return Model对象
	 */
	public function getModel() {
		if (! is_object ( $this->model )) {
			Mid::loadClass ( 'model' );
			$this->model = Model::Instance ();
		}
		return $this->model;
	}
	
	/**
	 * 创建一个Model对象
	 *
	 * @return Model对象
	 */
	public function Model($str) {
		static $_model = 0;
		static $__model = array ();
		if (strpos ( $str, '.' ) !== false) {
			list ( $modelName, $className ) = explode ( '.', $str );
		} else {
			$modelName = $str;
			$className = $str;
		}
		$className = $className . 'Model';
		if ($_model === 0) {
			Mid::loadClass ( 'model' );
			$_model = 1;
		}
		if (! isset ( $__model [$modelName] )) {
			Mid::load ( 'm/model/' . $modelName );
			$__model [$modelName] = 1;
		}
		if (class_exists ( $className )) {
			return new $className ( );
		} else {
			exit ( 'controller Error: Cound not found model class' );
		}
	}
	
	/*######################################################################
	* 
	* Data handle for control data
	* 
	* #####################################################################*/
	
	/**
	 * push data to data array
	 * 
	 * @param array $data
	 */
	function addData($data) {
		$this->data = array_merge ( $this->data, $data );
	}
	
	/**
	 * add data to array
	 * 
	 * @param $key
	 * @param $data
	 */
	function setData($key, $data) {
		$this->data [$key] = $data;
	}
	
	/**
	 * get data by key	
	 * 
	 * @param $key
	 * @return unknown_type
	 */
	function getData($key) {
		return $this->data [$key];
	}
	
	/**
	 * fetch all data
	 *
	 */
	function allData() {
		return $this->data;
	}
	
	/**
	 * Return instance of this class
	 *
	 * @return object this instance
	 */
	public function Instance() {
		static $instance = NULL;
		if (! $instance) {
			$instance = new Controller ( );
		}
		return $instance;
	}
}
?>